//-----------------------------------------------------------------------
// <copyright file="LienHeDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class LienHeDAOBase
    {
        #region Common methods
        public virtual LienHe CreateLienHeFromReader(IDataReader reader)
        {
            LienHe item = new LienHe();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("HoTen"))) item.HoTen = (string)reader["HoTen"];
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
                if (!reader.IsDBNull(reader.GetOrdinal("TieuDe"))) item.TieuDe = (string)reader["TieuDe"];
                if (!reader.IsDBNull(reader.GetOrdinal("NoiDung"))) item.NoiDung = (string)reader["NoiDung"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateLienHeFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateLienHe methods
            
        public virtual void CreateLienHe(LienHe lienHe)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLienHeInsert");
                
				database.AddInParameter(dbCommand, "@HoTen", DbType.StringFixedLength, lienHe.HoTen);
				database.AddInParameter(dbCommand, "@Email", DbType.StringFixedLength, lienHe.Email);
				database.AddInParameter(dbCommand, "@TieuDe", DbType.StringFixedLength, lienHe.TieuDe);
				database.AddInParameter(dbCommand, "@NoiDung", DbType.StringFixedLength, lienHe.NoiDung);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateLienHeException, ex);
            }
        }

        #endregion
		
        #region UpdateLienHe methods
        
        public virtual void UpdateLienHe(LienHe lienHe)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLienHeUpdate");            
                
                database.AddInParameter(dbCommand, "@HoTen", DbType.StringFixedLength, lienHe.HoTen);
                database.AddInParameter(dbCommand, "@Email", DbType.StringFixedLength, lienHe.Email);
                database.AddInParameter(dbCommand, "@TieuDe", DbType.StringFixedLength, lienHe.TieuDe);
                database.AddInParameter(dbCommand, "@NoiDung", DbType.StringFixedLength, lienHe.NoiDung);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateLienHeException, ex);
            }
        }
        
        #endregion
		
        #region DeleteLienHe methods
        public virtual void DeleteLienHe(string email)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLienHeDelete");
                
                database.AddInParameter(dbCommand, "@Email", DbType.StringFixedLength, email);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteLienHeException, ex);
            }
        }

        #endregion
		
        #region GetLienHe methods
        
        public virtual LienHe GetLienHe(string email)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLienHeGet");
                
                database.AddInParameter(dbCommand, "@Email", DbType.StringFixedLength, email);
                
                LienHe lienHe =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        lienHe = CreateLienHeFromReader(reader);
                        reader.Close();
                    }
                }
                return lienHe;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetLienHeException, ex);
            }
        }
        
        #endregion
		
        #region GetLienHeList methods
        public virtual LienHeCollection GetLienHeList(LienHeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spLienHeGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                LienHeCollection lienHeCollection = new LienHeCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        LienHe lienHe = CreateLienHeFromReader(reader);
                        lienHeCollection.Add(lienHe);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return lienHeCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetLienHeListException, ex);
            }
        }
        
        public virtual LienHeCollection GetLienHeList(LienHeColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetLienHeList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}